package com.ifonly.initialize;

import com.google.common.base.Strings;
import com.ifonly.activerecord.Model;
import com.ifonly.config.Configuration;
import com.ifonly.core.IfOnlyConfig;
import com.ifonly.ext.druid.DruidStatViewHandler;
import com.ifonly.handler.Handlers;
import com.ifonly.interceptor.GlobalInterceptors;
import com.ifonly.mvc.ActionMapper;
import com.ifonly.mvc.ActionViewHandler;
import com.ifonly.mvc.Controller;
import com.ifonly.mvc.helpers.ActionMappingHelper;
import com.ifonly.plugin.ActiveRecordPlugin;
import com.ifonly.plugin.DevPlugin;
import com.ifonly.plugin.Plugins;
import com.ifonly.security.PermissionValidationHandler;
import com.ifonly.utils.ReflectUtils;

import java.util.List;

/**
 * @author <a href="mailto:ifonlymaster@163.com">ifonly</a>
 * @version 1.0 2015-12-15 16:07
 * @since JDK 1.6
 */
public class IfOnlyAutoConfig extends IfOnlyConfig {

    private List<Class<?>> getClasses(Class<?> clazz) {
        return classContainer.group(clazz);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void configAction(ActionMapper actionMapper) {
        logger.debug("---configAction--");
        List<Class<?>> controllerClasses = getClasses(Controller.class);

        if (controllerClasses != null && !controllerClasses.isEmpty()) {
            for (Class<?> clazz : controllerClasses) {
                Class<? extends Controller> controllerClass = (Class<? extends Controller>) clazz;
                ActionMappingHelper.controllerActionMapping(controllerClass, actionMapper, false);
            }
        }
    }

    @Override
    public void configInterceptor(GlobalInterceptors interceptors) {
    }

    @SuppressWarnings("unchecked")
    @Override
    public void configPlugin(Plugins plugins) {
        if (Configuration.getInstance().isDevMode()) {
            plugins.add(new DevPlugin());
        }

        ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin();

        List<Class<?>> classes = classContainer.group(Model.class);
        for (Class<?> clz : classes) {
            activeRecordPlugin.addTableMapper((Class<? extends Model>) clz);
        }

        plugins.add(activeRecordPlugin);
    }

    @Override
    public void configHandler(Handlers handlers) {
        if (cfg.isDevMode()) {
            handlers.add(new ActionViewHandler());
        }
        if (cfg.isPermissionValidate()) {
            String permissionValidator = cfg.getProperty("permission.validator");
            PermissionValidationHandler.IPermissionValidator validator = null;
            if (!Strings.isNullOrEmpty(permissionValidator)) {
                validator = (PermissionValidationHandler.IPermissionValidator) ReflectUtils.newInstance(permissionValidator);
            }
            if (validator != null) {
                handlers.add(new PermissionValidationHandler(validator));
            } else {
                handlers.add(new PermissionValidationHandler());
            }

        }
        if (cfg.isDruidStat()) {
            handlers.add(new DruidStatViewHandler(cfg.getDruidPath()));
        }
    }

}
